package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainScoreTemplateNature;
import org.springblade.modules.admin.service.MaintainScoreTemplateNatureService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.admin.entity.MaintainScoreTemplate;
import org.springblade.modules.admin.mapper.MaintainScoreTemplateMapper;
import org.springblade.modules.admin.service.MaintainScoreTemplateService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MaintainScoreTemplateServiceImpl extends BaseServiceImpl<MaintainScoreTemplateMapper, MaintainScoreTemplate> implements MaintainScoreTemplateService{

	@Autowired
	private MaintainScoreTemplateNatureService scoreTemplateNatureService;

	@Override
	public MaintainScoreTemplate detail(Long id) {
		MaintainScoreTemplate scoreTemplate = super.getById(id);
		List<MaintainScoreTemplateNature> scoreTemplateNatures = scoreTemplateNatureService.list(Wrappers.<MaintainScoreTemplateNature>lambdaQuery().eq(MaintainScoreTemplateNature::getScoreTemplateId,id));
		scoreTemplate.setScoreTemplateNatures(scoreTemplateNatures);
		return scoreTemplate;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean submit(MaintainScoreTemplate entity) {
		super.saveOrUpdate(entity);
		scoreTemplateNatureService.remove(Wrappers.<MaintainScoreTemplateNature>lambdaQuery().eq(MaintainScoreTemplateNature::getScoreTemplateId,entity.getId()));
		if(Func.isNotEmpty(entity.getScoreTemplateNatures())){
			entity.getScoreTemplateNatures().forEach(e -> e.setScoreTemplateId(entity.getId()));
			scoreTemplateNatureService.saveBatch(entity.getScoreTemplateNatures());
		}
		return Boolean.TRUE;
	}

}
